<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Poll
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 * @version    $Id: Create.php 8110 2010-12-22 21:03:38Z char $
 * @author     Steve
 */

/**
 * @category   Application_Extensions
 * @package    Poll
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 */
class Poll_Form_Create extends Engine_Form {
	public function init() {
		$auth = Engine_Api::_ ()->authorization ()->context;
		$user = Engine_Api::_ ()->user ()->getViewer ();
		
		$this->setTitle ( 'Create Poll' )->setDescription ( 'Create your poll below, then click "Create Poll" to start your poll.' );
		
		$this->addElement ( 'text', 'title', array ('label' => 'Poll Title', 'required' => true, 'maxlength' => 63, 'filters' => array ('StripTags', new Engine_Filter_Censor (), new Engine_Filter_StringLength ( array ('max' => '63' ) ) ) ) );
		
		$this->addElement ( 'textarea', 'description', array ('label' => 'Description', 'filters' => array ('StripTags', new Engine_Filter_Censor (), new Engine_Filter_StringLength ( array ('max' => '400' ) ) ) ) );
		
		$this->addElement ( 'textarea', 'options', array ('label' => 'Possible Answers', 'style' => 'display:none;' ) );
		
		// Privacy 
		$availableLabels = array ('everyone' => 'Everyone', 'registered' => 'All Registered Members', 'owner_network' => 'Friends and Networks', 'owner_member_member' => 'Friends of Friends', 'owner_member' => 'Friends Only', 'owner' => 'Just Me' );
		
		// Init profile view
		$viewOptions = ( array ) Engine_Api::_ ()->authorization ()->getAdapter ( 'levels' )->getAllowed ( 'poll', $user, 'auth_view' );
		$viewOptions = array_intersect_key ( $availableLabels, array_flip ( $viewOptions ) );
		
		if (! empty ( $viewOptions ) && count ( $viewOptions ) >= 1) {
			// Make a hidden field
			if (count ( $viewOptions ) == 1) {
				$this->addElement ( 'hidden', 'auth_view', array ('value' => key ( $viewOptions ) ) );
			
		// Make select box
			} else {
				$this->addElement ( 'Select', 'auth_view', array ('label' => 'Privacy', 'description' => 'Who may see this poll?', 'multiOptions' => $viewOptions, 'value' => key ( $viewOptions ) ) );
				$this->auth_view->getDecorator ( 'Description' )->setOption ( 'placement', 'append' );
			}
		}
		
		// Comment
		// Init profile comment
		$commentOptions = ( array ) Engine_Api::_ ()->authorization ()->getAdapter ( 'levels' )->getAllowed ( 'poll', $user, 'auth_comment' );
		$commentOptions = array_intersect_key ( $availableLabels, array_flip ( $commentOptions ) );
		
		if (! empty ( $commentOptions ) && count ( $commentOptions ) >= 1) {
			// Make a hidden field
			if (count ( $commentOptions ) == 1) {
				$this->addElement ( 'hidden', 'auth_comment', array ('value' => key ( $commentOptions ) ) );
			
		// Make select box
			} else {
				$this->addElement ( 'Select', 'auth_comment', array ('label' => 'Comment Privacy', 'description' => 'Who may post comments on this poll?', 'multiOptions' => $commentOptions, 'value' => key ( $commentOptions ) ) );
				$this->auth_comment->getDecorator ( 'Description' )->setOption ( 'placement', 'append' );
			}
		}
		
		// Search
		$this->addElement ( 'Checkbox', 'search', array ('label' => "Show this poll in search results", 'value' => 1 ) );
		
		// Submit
		$this->addElement ( 'Button', 'submit', array ('label' => 'Create Poll', 'ignore' => true, 'decorators' => array ('ViewHelper' ), 'type' => 'submit' ) );
		
		$this->addElement ( 'Cancel', 'cancel', array ('prependText' => ' or ', 'label' => 'cancel', 'link' => true, //'href' => 'javascr',
		//'onclick' => 'parent.Smoothbox.close();',
		'decorators' => array ('ViewHelper' ) ) );
		
		$this->addDisplayGroup ( array ('submit', 'cancel' ), 'buttons', array ('decorators' => array ('FormElements', 'DivDivDivWrapper' ) ) );
	}
}